<template>
  <!--模态窗 单行-->
  <transition
    name="custom-classes-transition"
    enter-active-class="animated fadeInDown"
    leave-active-class="animated fadeOutUp"
  >
    <div class="modal-ctrl-body" v-show="addRoleObj.isShow">
    <div class="modal-ctrl-main odd-row-ctrl">
      <div class="modal-title">
        <p>新增角色信息</p>
      </div>
      <div class="modal-ctrl-content">

        <div class="modal-row">
          <div class="modal-form-key f-l indispensable">
            角色名称：
          </div>
          <input type="text" maxlength="16" placeholder="最长16个字符" v-model="roleParams.name" class="modal-text f-l">
        </div>

        <div class="modal-row">
          <div class="modal-form-key f-l">
            等级：
          </div>
          <select @change="selectStageCode" :value="roleParams.stageCode">
            <option value="1">市局</option>
            <option value="2">分局</option>
            <option value="3">派出所/警务站</option>
          </select>
        </div>

        <div class="modal-row">
          <div class="modal-form-key f-l">
            等级排序：
          </div>
          <input type="text" maxlength="4" placeholder="最长4位字符" v-model="roleParams.pricode" class="modal-text f-l" max="9999" min="0">
        </div>

        <div class="modal-row">
          <div class="modal-form-key f-l">
            备注：
          </div>
          <textarea maxlength="50" placeholder="最长50个字符" v-model="roleParams.remark" class="modal-text f-l"></textarea>
        </div>

      </div>
      <div class="btns text-r">
        <button type="button" class="btn btn-default" @click="closeRoleModal">取消</button>
        <button type="button" class="btn btn-info" @click="roleAddFn">确定</button>
      </div>
    </div>
  </div>
  </transition>
</template>

<script>
    export default {
        name: "RoleAddModal",
        props:["addRoleObj"],
        data(){
          return{
            roleParams:{
              name:"",//角色名称
              stageCode:"1",//等级
              pricode:"",//等级排序
              remark:""//备注
            }
          }
        },
        methods:{
          //新增角色
          roleAddFn(){

            let params = {
              "columns": [],
              "dataTypeCode": "userprofile",
              "datas": [],
              "tableName": "T_PRIV_ROLE"
            };

            if( this.verifyForm.isNull(this.roleParams.name) ){
              this.s_alert("角色名称不能为空！");
              // alert("角色名称不能为空！");
              return false;
            }else if( this.verifyForm.isNumber(this.roleParams.pricode)  ){
              this.s_alert("排序等级只能为整数！");
              // alert("等级排序过长");
              return false;
            }

            let roleQueryParmas = {
              "columns": [
                "*"
              ],
              "conditions": [
                {
                  "field": "name",
                  "symbol": "=",
                  "value": this.roleParams.name
                }
              ],
              "dataTypeCode": "userprofile",
              "pageSize": -1,
              "tableName": "T_PRIV_ROLE"
            };

            this.$axios.post(this.HOST+"/base/query/page",roleQueryParmas)
              .then(data=>{
                //console.log(data);
                if( data.data.data.list.length > 0 ){
                  this.s_alert("该角色名称已经存在！");
                  // alert("该角色名称已经存在！");
                }else{

                  for( let roleKey in this.roleParams ){
                    params["columns"].push(roleKey);
                    params["datas"].push(this.roleParams[roleKey]);
                  }
                  //console.log(params);
                  this.$axios.post(this.HOST+"/base/save",params)
                    .then((data)=>{
                      //console.log(data);
                      if( data.data.success === true ){
                        this.s_alert("新增角色成功！");
                        // alert("新增角色成功！");
                        this.s_logwrite("基础信息管理-角色管理","新增角色");
                        this.closeRoleModal();
                      }
                    })
                    .catch((err)=>{
                      this.s_alert("新增角色失败！");
                      // alert("新增角色失败！");
                      //console.log(err);
                    });

                }
              })
              .catch(err=>{
                //console.log(err);
              });

            ////console.log(this.roleParams);
          },
          //选择等级
          selectStageCode(ele){
            this.roleParams.stageCode = ele.target.value;
          },
          //清除缓存
          clearData(){
            this.roleParams={
              name:"",//角色名称
              stageCode:"1",//等级
              pricode:"",//等级排序
              remark:""//备注
            }
          },
          //关闭弹窗
          closeRoleModal(){
            this.clearData();
            this.$emit("closeRoleAdd");
          }
        }
    }
</script>

<style scoped>
  textarea.modal-text {
    min-height: 60px;
    max-height: 60px;
    max-width: 80%;
    min-width: 80%;
  }
  .fadeInDown,.fadeOutUp{
    animation-duration: .500s;
  }
</style>
